package com.oi.test01.hw.tiMu10;

import com.oi.test01.entity.Demo1;

import java.lang.reflect.Field;
import java.sql.*;

/**
 * @author bin
 * @date 2025 - 03 -19
 **/
public class DbUtil {
    /**
     * 连接驱动程序
     */
    private static final String DRIVER = "com.mysql.cj.jdbc.Driver";

    /**
     * 连接URL
     */
    private static final String URL = "jdbc:mysql://127.0.0.1:3307/ch01?useUnicode=true;characterEncoding=utf8;serverTimezone=Asia/Shanghai";

    /**
     * 帐号
     */
    private static final String USER = "root";

    /**
     * 密码
     */
    private static final String PASS = "root";

    // 静态代码块：在类加载时，实现一些操作
    static {
        try {
            // 加载驱动程序
            Class.forName(DRIVER);
        } catch (ClassNotFoundException e) {
            System.out.println("加载驱动程序失败...");
            e.printStackTrace();
        }
    }

    /**
     * 获取连接对象 -- Java程序 与 数据库之间的桥梁
     *
     * @return
     */
    public static Connection getConnection() {
        Connection conn = null;
        try {
            conn = DriverManager.getConnection(URL, USER, PASS);
        } catch (SQLException e) {
            System.out.println("获取连接对象失败...");
            e.printStackTrace();
        }
        return conn;
    }


    /**
     * 关闭相关的 JDBC 对象
     * <p>
     * DriverManager：驱动管理对象，获取连接对象
     * <p>
     * DriverManager.getConnection(URL, USER, PASS);
     * <p>
     * ResultSet：结果集对象 用于接收查询数据时，返回的结果
     * <p>
     * Statement：语句对象 用于执行SQL语句（PreparedStatement、CallableStatement）
     * 增、删、改：executeUpdate() 查询：executeQuery()
     * <p>
     * <p>
     * Connection：连接对象 建立JAVA程序与数据库之间的桥梁
     *
     * @param rst
     * @param stmt 父类对象可以接收子类对象 - 多态
     * @param conn
     */
    public static void close(ResultSet rst, Statement stmt, Connection conn) {
        if (rst != null) {
            try {
                rst.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if (stmt != null) {
            try {
                stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public static int insert(Object obj) throws Exception {
        Class<?> clazz = obj.getClass();
        String sql = CreateSql.createSql(clazz);
        Connection connection = getConnection();
        PreparedStatement pst = null;
        int count = 0;

        Field[] fields = clazz.getDeclaredFields();
        pst = connection.prepareStatement(sql);
        for (int i = 0; i < fields.length; i++) {
            fields[i].setAccessible(true);
            Object value = fields[i].get(obj);
            pst.setObject(i + 1, value);
        }

         count = pst.executeUpdate();

        return count;
    }

    public static void main(String[] args) throws Exception {
        Demo1 d = new Demo1("张三", 18, "男");
        int count = insert(d);
        System.out.println(count);
    }
}
